package com.example.weatherapp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {
    private EditText cityEditText;
    private Button searchButton;
    private LinearLayout weatherContainer;
    private ProgressBar progressBar;
    private TextView cityTextView, tempTextView, weatherDescTextView;
    private TextView humidityTextView, windTextView, pressureTextView;
    private ImageView weatherIconImageView;

    private final String API_KEY = "e4a91b5d5600dc457b41d8f1ee9dc92a";
    private final String UNITS = "metric"; // 使用摄氏度
    private final String LANGUAGE = "zh_cn"; // 中文显示

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

        searchButton.setOnClickListener(v -> {
            String city = cityEditText.getText().toString().trim();
            if (!city.isEmpty()) {
                fetchWeatherData(city);
            } else {
                Toast.makeText(MainActivity.this, "请输入城市名称", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initViews() {
        cityEditText = findViewById(R.id.cityEditText);
        searchButton = findViewById(R.id.searchButton);
        weatherContainer = findViewById(R.id.weatherContainer);
        progressBar = findViewById(R.id.progressBar);

        cityTextView = findViewById(R.id.cityTextView);
        tempTextView = findViewById(R.id.tempTextView);
        weatherDescTextView = findViewById(R.id.weatherDescTextView);
        humidityTextView = findViewById(R.id.humidityTextView);
        windTextView = findViewById(R.id.windTextView);
        pressureTextView = findViewById(R.id.pressureTextView);
        weatherIconImageView = findViewById(R.id.weatherIconImageView);
    }

    private void fetchWeatherData(String city) {
        showLoading(true);

        WeatherApiService apiService = RetrofitClient.getApiService();
        Call<WeatherResponse> call = apiService.getCurrentWeather(city, UNITS, API_KEY, LANGUAGE);

        call.enqueue(new Callback<WeatherResponse>() {
            @Override
            public void onResponse(Call<WeatherResponse> call, Response<WeatherResponse> response) {
                showLoading(false);

                if (response.isSuccessful() && response.body() != null) {
                    WeatherResponse weatherResponse = response.body();
                    updateWeatherUI(weatherResponse);
                } else {
                    Toast.makeText(MainActivity.this, "获取天气数据失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<WeatherResponse> call, Throwable t) {
                showLoading(false);
                Toast.makeText(MainActivity.this, "网络错误: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void updateWeatherUI(WeatherResponse weatherResponse) {
        weatherContainer.setVisibility(View.VISIBLE);

        // 设置城市和国家
        String cityCountry = weatherResponse.getName() + ", " + weatherResponse.getSys().getCountry();
        cityTextView.setText(cityCountry);

        // 设置温度
        String temp = String.format("%.1f°C", weatherResponse.getMain().getTemp());
        tempTextView.setText(temp);

        // 设置天气描述
        if (weatherResponse.getWeather().length > 0) {
            String description = weatherResponse.getWeather()[0].getDescription();
            weatherDescTextView.setText(description);

            // 加载天气图标
            String iconUrl = "https://openweathermap.org/img/wn/" +
                    weatherResponse.getWeather()[0].getIcon() + "@2x.png";
            Glide.with(this).load(iconUrl).into(weatherIconImageView);
        }

        // 设置湿度
        humidityTextView.setText("湿度\n" + weatherResponse.getMain().getHumidity() + "%");

        // 设置风速
        windTextView.setText("风速\n" + weatherResponse.getWind().getSpeed() + " m/s");

        // 设置气压
        pressureTextView.setText("气压\n" + weatherResponse.getMain().getPressure() + " hPa");
    }

    private void showLoading(boolean isLoading) {
        if (isLoading) {
            progressBar.setVisibility(View.VISIBLE);
            weatherContainer.setVisibility(View.GONE);
        } else {
            progressBar.setVisibility(View.GONE);
        }
    }
}